﻿using System;

using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls.Base;
using Silvermoon.Visuals;
using System.Drawing;

namespace Silvermoon.Controls
{
    /// <summary>
    /// Control that decorates a control with a background.
    /// </summary>
    public class BackgroundDecorator : Decorator
    {
        private RectangleVisual background;
        private Color color = Color.Empty;

        public override void Dispose()
        {
            if (background != null) background.Dispose();
            Visuals = null;
            base.Dispose();
        }

        public Color Color
        {
            get { return color; }
            set { if (color != value) { color = value; Invalidate(InvalidationFlag.Color); } }
        }

        protected override void OnLayout()
        {
            if (CheckFlag(InvalidationFlag.Color))
            {
                if (color != Color.Empty)
                {
                    if (background == null) background = new RectangleVisual();
                    background.Color = Color;
                    Visuals = new Visual[] { background };
                }
                else
                {
                    Visuals = null;
                }
            }
            if (background != null) background.Size = Size;
            base.OnLayout();
        }

    }
}
